<?php

namespace Wechat\Controller;
/**
* 
*/
class ArticleController extends BaseController
{
	//文章列表
    public function index($content = ''){
        $where = array();
        $keywords=$_POST['keywords'];
        //$where['title'] = $keywords ? array('like','%'.$keywords.'%') : array('neq','');
        if ($keywords) {
            $where['title'] = array('like','%'.$keywords.'%');
        }
        $dataPage = $this->getPager(M('WxArticle'),$where);
        $this->assign('list',$dataPage['data']);
        $this->assign('page',$dataPage['page']);
        $this->display();
    }
    //文章分类列表
    public function sort_list($content = ''){
        $where = array();
        $keywords=$_POST['keywords'];
        //$where['title'] = $keywords ? array('like','%'.$keywords.'%') : array('neq','');
        if ($keywords) {
            $where['tag_name'] = array('like','%'.$keywords.'%');
        }
        $dataPage = $this->getPager(M('WxArtisort'),$where);
        $this->assign('list',$dataPage['data']);
        $this->assign('page',$dataPage['page']);
        $this->display();
    }
    //评论列表
    public function discuss_list(){
        $where = array();
        $dataPage = $this->getPager(M('WxArticle_discuss'),$where);
        $this->assign('list',$dataPage['data']);
        $this->assign('page',$dataPage['page']);
        $this->display();

    }
    //删除评论
    public function discuss_delete($content = ''){

        $del=M('WxArticle_discuss')->delete($_GET['id']);
        $del == false ? $this->error('删除失败') : $this->success('删除成功');

        // $res = M('WxArticle')->delete($_GET['id']);
        // $res == false ? $this->error('删除失败') : $this->success('删除成功');

    }
    //添加文章
    public function adds(){
        $Asort = M('WxArtisort')->select();
        $this->assign('sortlist',$Asort);
        $this->display();
    }
    public function adds_ok(){        
        if(IS_POST){
            $data = array(
                'title' => $_POST['title'],
                'content' => $_POST['content'],
                'add_time' => time(),
                'modify_time' => time(),
                'status'=>$_POST['arti_status'],
                'article_sort'=>$_POST['article_sort'],
                'author'=>session('usernamea')
                );
            //判断标题是否已存在
            $arti_name=(bool)(M('WxArticle')->where(array('title'=>$_POST['title']))->select());
            if($arti_name==false||$arti_name==""){
            //找出文章的标题，增加它的分类数量,存入
             $sortdata['tag_name']=$data['article_sort'];
             $sort=M('WxArtisort')->where($sortdata)->select();
             $sortnum=(int)($sort[0]['article_num']);
             $sort[0]['article_num']=(string)($sortnum+1);
             $arr = array(
                'Id' => $sort[0]['id'],
                'tag_name' => $sortdata['tag_name'],
                'article_num' => $sort[0]['article_num']
                );
             
             $fin=M('WxArtisort')->data($arr)->save();
            if(M('WxArticle')->data($data)->add()) {
                    $this -> success('发布成功!');
            }else{
                $this -> error('发布失败!请重试...');
                }
            }else{
                $this -> error('发布失败!文章标题已存在！');
            }
        }
    }
    //修改文章
    public function edit($content = ''){
        $keywords['id'] = I('get.id', 0, 'intval');
        $art_info = M('WxArticle')->where($keywords)->find();
        $this->assign('art_info', $art_info);
        //分类模型实例化
        $Asort = M('WxArtisort')->select();
        $this->assign('sortlist', $Asort);
        $this->display();
        
    }
    public function edit_ok($content = ''){
        if (!IS_POST) {
            exit('hack');
        }
        $id = I('post.id', 0, 'intval');
        $exist = M('WxArticle')->where(['id' => $id, 'status' => 1])->find();
        if (empty($exist)) {
            $this->error('文章不存在');
        }
        $update = [
            'title' => I('post.title', ''),
            'content' => I('post.content', ''),
            'status' =>  I('post.status', '1', 'intval'),
            'sort' => I('post.sort', '', 'intval'),
            'modify_time' => time(),
        ];
        $resp = M('WxArticle')->where(['id' => $id, 'status' => 1])->save($update);
        if ($resp !== false) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }
    //删除文章
    public function delete($content = ''){
        $res = M('WxArticle')->delete($_GET['id']);
        $res == false ? $this->error('删除失败') : $this->success('删除成功');
    }
    //添加分类
    public function sort_adds($content = ''){
        $this->display();
    }
    public function sort_adds_ok($content = ''){
        if(IS_POST){
            $data = array(
                'tag_name' => $_POST['tag_name'],
                'add_time' => time(),
                'modify_time' => time(),
                'article_num'=> 0,
                'author'=>session('usernamea')
                );
            // dump($data);
            // die();
        $sort_name=(bool)(M('WxArtisort')->where(array('tag_name'=>$_POST['tag_name']))->select());
            if($sort_name==false||$sort_name==""){
                if(M('WxArtisort')->data($data)->add()) {
                        $this -> success('添加成功!');
                }else{
                    $this -> error('添加失败!请重试...');
                }
            }else{
                 $this -> error('添加失败!类名已存在！请重试...');
            }
        }
    }
    //修改分类名
    public function sort_revise($content = ''){
        $keywords['Id']=$_GET['id'];
        $Arti = M('WxArtisort')->where($keywords)->select();
        $this->assign('list',$Arti);
        $this->display();
    }
    public function sort_revise_ok($content = ''){
    if(IS_POST){
            $data = array(
                'Id' => $_POST['sort_id'],
                'tag_name' => $_POST['tag_name'],
                'modify_time' => time(),
                'author'=>session('usernamea')
                );
            $sort_name=(bool)(M('WxArtisort')->where(array('tag_name'=>$_POST['tag_name']))->select());
            if($sort_name==false||$sort_name==""){
               if(M('WxArtisort')->data($data)->save()) {
                    $this -> success('更新成功!');
                }else{
                    $this -> error('更新失败!请重试...');
                }
            }else{
                 $this -> error('添加失败!类名已存在！请重试...');
            }
        }
    }
    //删除分类名
    public function sort_delete($content = ''){
        $kee['Id']=$_GET['id'];
        $data=M('WxArtisort')->where($kee)->select();
        //echo $data[0]['article_num'];
        if($data[0]['article_num']==''||$data[0]['article_num']==0){
        $res = M('WxArtisort')->where($kee)->delete($_GET['id']);
        $res == false ? $this->error('删除失败!') : $this->success('删除成功!');
        }else{
            $this->error('删除失败,请先清空该类内数据！');
        }
    }
    //分类查看文章列表
    public function sortlist_cont($content = ''){
        $where = array();
        $where['Id']=$_GET['id'];
        $data=M('WxArtisort')->where($where)->select();

        $keywords=$_POST['keywords'];
        if ($keywords) {
            $arti['title'] = array('like','%'.$keywords.'%');
            // echo $arti; 
        }
        $arti['article_sort']=$data[0]['tag_name'];
        $dataPage = $this->getPager(M('WxArticle'),$arti);
        // $aa=M('WxArticle')->getlastSql();
        $this->assign('list',$dataPage['data']);
        $this->assign('page',$dataPage['page']);
        $this->display();
    }
}